import axios from 'axios'
import {
  ref
} from 'vue'

export default function useUrlLoader<T>(url: string) {
  // 定义响应式数据
  const loading = ref(false)
  const result = ref<T>()
  const errorMsg = ref()

  loading.value = true
  axios.get(url).then(
    response => {
      loading.value = false
      result.value = response.data
    },
    error => {
      loading.value = false
      errorMsg.value = error.message || '请求错误'
    }
  )

  return {
    loading,
    result,
    errorMsg
  }
}